const router = require('koa-router')()
const query = require("../db/mysql");
const fs = require("fs");
const path = require("path");
const {obj,encrypt, decrypt} = require('../public/util')
const {key,iv} = obj;

router.get('/pInformation', async (ctx, next) => {
  try {
    let sql = `SELECT pInformation FROM painting where pId= ?`
    let data = await query(sql, [ctx.query.pId]);
    return ctx.body = {
      status: 200,
      data: data,
      msg: ''
    }
  } catch (error) {
    return ctx.body = {
      status: 500,
      msg: error
    }
  }
})

router.get('/pContent', async (ctx, next) => {
  try {
    let sql = `select pContent from painting where pId = ?`;
    let data = await query(sql, [ctx.query.pId]);
    var path1 = __dirname.slice(0, -6);
    var filePath = path.join(path1, `/public/data/${data[0]['pContent']}`);
    var file = fs.readFileSync(filePath);
    return ctx.body = file;
  } catch (error) {
    return ctx.body = {
      status: 500,
      msg: error
    }
  }
})

router.get('/pName', async (ctx, next) => {
  try {
    let sql = `select pName from painting where pId = ?`;
    let data = await query(sql, [ctx.query.pId]);
    return ctx.body = {
      status: 200,
      data: data,
      msg: ''
    }
  } catch (error) {
    return ctx.body = {
      status: 500,
      msg: error
    }
  }
})


router.get('/painting', async (ctx, next) => {
  try {
    let key = Object.keys(ctx.query)[0];
    let sql = `SELECT pId,pInformation FROM painting where ${key} = ?`;
    let data = await query(sql,[ctx.query[key]]);
    return ctx.body = {
      status: 200,
      data: data,
      msg: ''
    }
  } catch (error) {
    return ctx.body = {
      status: 500,
      msg: error
    }
  }
})

router.get('/author', async (ctx, next) => {
  try {
    let sql = `SELECT aId,aInformation FROM author where aName = ?`;
    let data = await query(sql,[ctx.query.aName]);
    let sql1 = `SELECT pId FROM painting where aId = ?`;
    let data1 = await query(sql1,[data[0].aId]);
    return ctx.body = {
      status: 200,
      data: {aInfo:data[0].aInformation,pId:data1},
      msg: ''
    }
  } catch (error) {
    return ctx.body = {
      status: 500,
      msg: error
    }
  }

})

//提交用户收藏的画作
router.post('/collection', async (ctx, next)=>{
    try{
        var {pId,uId} = ctx.request.body
        uId = decrypt(uId,key,iv);
        let sql = `INSERT INTO collection(uId,pId) VALUES (?,?)`;
        await query(sql,[uId,pId])
        ctx.status = 200
        return ctx.body = {
            status:200,
            msg:'insert success'
        }
    }catch(err){
        console.log(err)
        return ctx.body = {
            status:500,
            msg:err
        }  
    }
})
//删除用户取消收藏的画作
router.delete('/collection', async (ctx, next)=>{
  try{
      // var {pId,uId} = ctx.request.body
      // console.log(pId,uId);
      var uId = ctx.query.uId;
      uId = decrypt(uId,key,iv);
      let sql = `delete from collection where pId=? and uId=? `;
      await query(sql,[ctx.query.pId,uId])
      ctx.status = 200
      return ctx.body = {
          status:200,
          msg:'delete success'
      }
  }catch(err){
      console.log(err)
      return ctx.body = {
          status:500,
          msg:err
      }  
  }
})

module.exports = router
